package com.situ.mall.web.controller;

import com.situ.mall.common.entity.Member;
import com.situ.mall.common.entity.Worker;
import com.situ.mall.common.exception.MyException;
import com.situ.mall.common.service.ILoginService;
import com.situ.mall.common.service.IMemberService;
import com.situ.mall.common.service.IWorkerService;
import com.situ.mall.common.util.StringUtil;
import com.situ.mall.common.vo.ResultVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
@RequestMapping("/client")
public class LoginController {

    @Resource
    private ILoginService loginService;

    @Resource
    private IMemberService memberService;

    @Resource
    private IWorkerService workerService;

    //登陆
    @PostMapping("/login")
    public ResultVO login(String tel, String password, String type) {
        return loginService.check(tel, password, type);
    }

    //注册 tel & password & type必传，头像和昵称不必传
    @PostMapping("/register")
    public ResultVO register(String avatar, String nickname, String tel, String password, String type, String name) {
        return loginService.register(avatar, nickname, tel, password, type, name);
    }

    //修改密码
    @PostMapping("/changePassword")
    public ResultVO changePassword(String type, Integer id, String password) {
        if (Objects.equals(type, "1")) {
            Member member = new Member();
            member.setId(id);
            member.setPassword(StringUtil.md5(password));
            memberService.updateById(member);
            return ResultVO.data(200, "请求成功", memberService.getById(id));
        } else if (Objects.equals(type, "2")) {
            Worker worker = new Worker();
            worker.setId(id);
            worker.setPassword(StringUtil.md5(password));
            workerService.updateById(worker);
            return ResultVO.data(200, "请求成功", workerService.getById(id));
        }
        throw new MyException("修改失败");
    }
}
